package com.huluobo.high3demo.home;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.adapter.CommonPagerAdapter;
import com.huluobo.high3demo.databinding.FragmentHomeBinding;
import com.huluobo.high3demo.entity.TabEntity;
import com.huluobo.high3demo.home.items.ItemFragment;
import com.huluobo.mvvm.view.BaseFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseFragment<FragmentHomeBinding> {
    //tab标签的集合
    private ArrayList<CustomTabEntity> tabs = new ArrayList<>();

    //fragment的集合
    private List<Fragment> fragmentList = new ArrayList<>();

    private CommonPagerAdapter commonPagerAdapter;

    @Override
    protected void initData() {
        tabs.clear();
        fragmentList.clear();

        tabs.add(new TabEntity("推荐", 0, 0));
        tabs.add(new TabEntity("美食", 0, 0));
        tabs.add(new TabEntity("财经", 0, 0));
        tabs.add(new TabEntity("军事", 0, 0));
        tabs.add(new TabEntity("文化", 0, 0));
        tabs.add(new TabEntity("教育", 0, 0));
        tabs.add(new TabEntity("视频", 0, 0));
        tabs.add(new TabEntity("新闻", 0, 0));

        dataBinding.homeMainTabs.setTabData(tabs);

        for (int i = 0; i < tabs.size(); i++) {
            fragmentList.add(new ItemFragment());
        }

        commonPagerAdapter = new CommonPagerAdapter(getActivity(), fragmentList);
        dataBinding.homeVp2.setAdapter(commonPagerAdapter);
        //设置vp的缓存大小
        dataBinding.homeVp2.setOffscreenPageLimit(1);
    }

    @Override
    protected void initView() {
        dataBinding.homeVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                dataBinding.homeMainTabs.setCurrentTab(position);
            }
        });

        dataBinding.homeMainTabs.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.homeVp2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
}